package Views;

import Controllers.CheckpointController;
import Controllers.RoundController;
import Models.Checkpoint;
import Models.Location;
import Models.Round;
import Views.TableModels.CheckpointTableModel;
import Views.TableModels.RoundTableModel;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;

public class RoundPanel extends ViewAbstract<Round>
{
	private Location chosenLocation;
	
    public RoundPanel(SearchableTabbedPane parent)
	{
		initComponents();
		this.controller = new RoundController();
		this.tabbedPane = parent;
		checkpointTable.getSelectionModel().addListSelectionListener(this);
		checkpointTable.addMouseListener(new java.awt.event.MouseAdapter(){
			@Override
			public void mouseClicked(java.awt.event.MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(checkpointTable.getSelectedRowCount() == 1)
					{
						showLocationBActionPerformed(null);
					}
				}
			}
		});
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        roundNoL = new javax.swing.JLabel();
        locationNoL = new javax.swing.JLabel();
        roundNoTF = new javax.swing.JTextField();
        locationNoTF = new javax.swing.JTextField();
        showLocationB = new javax.swing.JButton();
        searchB = new javax.swing.JButton();
        searchTF = new javax.swing.JTextField();
        sep1 = new javax.swing.JSeparator();
        addCheckpointB = new javax.swing.JButton();
        removeCheckpointB = new javax.swing.JButton();
        Sep2 = new javax.swing.JSeparator();
        showAllB = new javax.swing.JButton();
        Sep3 = new javax.swing.JSeparator();
        newB = new javax.swing.JButton();
        editB = new javax.swing.JButton();
        deleteB = new javax.swing.JButton();
        cancelB = new javax.swing.JButton();
        saveB = new javax.swing.JButton();
        checkpointSP = new javax.swing.JScrollPane();
        checkpointTable = new javax.swing.JTable();
        longitudeL = new javax.swing.JLabel();
        latitudeL = new javax.swing.JLabel();
        longitudeTF = new javax.swing.JTextField();
        latitudeTF = new javax.swing.JTextField();
        descriptionL = new javax.swing.JLabel();
        descriptionTF = new javax.swing.JTextField();
        chooseLocationB = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(600, 420));
        setMinimumSize(new java.awt.Dimension(600, 420));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(600, 420));

        roundNoL.setText("Runde nr.:");
        roundNoL.setName("roundNoL"); // NOI18N

        locationNoL.setText("Lokation nr.:");
        locationNoL.setName("locationNoL"); // NOI18N

        roundNoTF.setEnabled(false);
        roundNoTF.setMaximumSize(new java.awt.Dimension(200, 20));
        roundNoTF.setMinimumSize(new java.awt.Dimension(200, 20));
        roundNoTF.setName("roundNoTF"); // NOI18N
        roundNoTF.setPreferredSize(new java.awt.Dimension(200, 20));

        locationNoTF.setEnabled(false);
        locationNoTF.setName("locationNoTF"); // NOI18N

        showLocationB.setText("Vis");
        showLocationB.setEnabled(false);
        showLocationB.setName("showLocationB"); // NOI18N
        showLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showLocationBActionPerformed(evt);
            }
        });

        searchB.setText("Søg");
        searchB.setName("searchB"); // NOI18N
        searchB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBActionPerformed(evt);
            }
        });

        searchTF.setMaximumSize(new java.awt.Dimension(200, 20));
        searchTF.setMinimumSize(new java.awt.Dimension(200, 20));
        searchTF.setName("searchTF"); // NOI18N
        searchTF.setPreferredSize(new java.awt.Dimension(200, 20));

        sep1.setName("sep1"); // NOI18N

        addCheckpointB.setText("Nyt");
        addCheckpointB.setEnabled(false);
        addCheckpointB.setName("addCheckpointB"); // NOI18N
        addCheckpointB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCheckpointBActionPerformed(evt);
            }
        });

        removeCheckpointB.setText("Fjern");
        removeCheckpointB.setEnabled(false);
        removeCheckpointB.setName("removeCheckpointB"); // NOI18N
        removeCheckpointB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeCheckpointBActionPerformed(evt);
            }
        });

        Sep2.setName("Sep2"); // NOI18N

        showAllB.setText("Vis Alle");
        showAllB.setName("showAllB"); // NOI18N
        showAllB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllBActionPerformed(evt);
            }
        });

        Sep3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        Sep3.setName("Sep3"); // NOI18N
        Sep3.setPreferredSize(new java.awt.Dimension(5, 23));

        newB.setText("Ny");
        newB.setName("newB"); // NOI18N
        newB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newBActionPerformed(evt);
            }
        });

        editB.setText("Rediger");
        editB.setEnabled(false);
        editB.setName("editB"); // NOI18N
        editB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editBActionPerformed(evt);
            }
        });

        deleteB.setText("Slet");
        deleteB.setEnabled(false);
        deleteB.setName("deleteB"); // NOI18N
        deleteB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBActionPerformed(evt);
            }
        });

        cancelB.setText("Annullér");
        cancelB.setEnabled(false);
        cancelB.setName("cancelB"); // NOI18N
        cancelB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBActionPerformed(evt);
            }
        });

        saveB.setText("Gem");
        saveB.setEnabled(false);
        saveB.setName("saveB"); // NOI18N
        saveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBActionPerformed(evt);
            }
        });

        checkpointSP.setName("checkpointSP"); // NOI18N

        checkpointTable.setModel(new RoundTableModel());
        checkpointTable.setEnabled(false);
        checkpointTable.setName("checkpointTable"); // NOI18N
        checkpointSP.setViewportView(checkpointTable);

        longitudeL.setText("Længde:");
        longitudeL.setName("longitudeL"); // NOI18N

        latitudeL.setText("Bredde:");
        latitudeL.setName("latitudeL"); // NOI18N

        longitudeTF.setEnabled(false);
        longitudeTF.setName("longitudeTF"); // NOI18N

        latitudeTF.setEnabled(false);
        latitudeTF.setName("latitudeTF"); // NOI18N

        descriptionL.setText("Note:");
        descriptionL.setName("descriptionL"); // NOI18N

        descriptionTF.setEnabled(false);
        descriptionTF.setName("descriptionTF"); // NOI18N

        chooseLocationB.setText("Vælg");
        chooseLocationB.setEnabled(false);
        chooseLocationB.setName("chooseLocationB"); // NOI18N
        chooseLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseLocationBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Sep2, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(showAllB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                        .addComponent(cancelB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveB))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(checkpointSP, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(roundNoL)
                                    .addComponent(locationNoL))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(roundNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(locationNoTF)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(chooseLocationB)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(showLocationB)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchB))
                            .addComponent(sep1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(descriptionL)
                                    .addGap(16, 16, 16))
                                .addComponent(longitudeL))
                            .addComponent(latitudeL))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(longitudeTF, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(descriptionTF)
                                .addComponent(latitudeTF, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addCheckpointB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeCheckpointB)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(searchB)
                            .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sep1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(roundNoL)
                            .addComponent(roundNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(locationNoL)
                            .addComponent(locationNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(showLocationB)
                            .addComponent(chooseLocationB))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkpointSP, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(longitudeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(longitudeL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(latitudeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(latitudeL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(descriptionTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(descriptionL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addCheckpointB)
                    .addComponent(removeCheckpointB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Sep2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(showAllB)
                    .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(newB)
                        .addComponent(editB)
                        .addComponent(saveB)
                        .addComponent(cancelB)
                        .addComponent(deleteB)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void newBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newBActionPerformed
	{//GEN-HEADEREND:event_newBActionPerformed
		clearComponents();
		changeComponentState(true);
		newOne = new Round();
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_newBActionPerformed

	private void editBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editBActionPerformed
	{//GEN-HEADEREND:event_editBActionPerformed
		changeComponentState(true);
		newB.setEnabled(true);
		editB.setEnabled(false);
	}//GEN-LAST:event_editBActionPerformed
	
	private void deleteBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBActionPerformed
		if(confirmDeletion("runden"))
		{
			controller.delete(chosenOne.getRoundNo());
			clearComponents();
		}
	}//GEN-LAST:event_deleteBActionPerformed

	private void saveBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveBActionPerformed
	{//GEN-HEADEREND:event_saveBActionPerformed
		Round theOne = getTheOne();

		try
		{
			theOne.setData(chosenLocation);

			if(theOne.getCheckpoints().size() == 0)
			{
				if(0 == JOptionPane.showConfirmDialog(this, "Runden har ingen checkpoints. Vil du slette runden?", "Ingen checkpoints?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE))
				{
					if(chosenOne != null)
					{
						controller.delete(theOne.getRoundNo());
					}
					clearComponents();
					changeComponentState(false);
					newB.setEnabled(true);
					return;
				}
			}

			if(chosenOne == null)
			{
				controller.insert(theOne);
			}
			else
			{
				controller.update(theOne, "roundNo = "+chosenOne.getRoundNo());
			}
		}
		catch(Models.EmptyValueException e)
		{
			informAboutEmptyFields();
			return;
		}

		informSavingWentOk("Runden");
		clearComponents();
		changeComponentState(false);
		newB.setEnabled(true);
	}//GEN-LAST:event_saveBActionPerformed
	
	private void showLocationBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showLocationBActionPerformed
		tabbedPane.switchTabWithNo(TabIndex.Locations, chosenLocation.getLocationNo());
	}//GEN-LAST:event_showLocationBActionPerformed

	private void showAllBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllBActionPerformed
		tabbedPane.search(TabIndex.Rounds, "*");
	}//GEN-LAST:event_showAllBActionPerformed

	private void addCheckpointBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addCheckpointBActionPerformed
	{//GEN-HEADEREND:event_addCheckpointBActionPerformed
		if(addCheckpointB.getText().equalsIgnoreCase("Nyt"))
		{
			longitudeTF.setEnabled(true);
			latitudeTF.setEnabled(true);
			descriptionTF.setEnabled(true);
			addCheckpointB.setText("Gem");
		}
		else
		{
			try
			{
				Checkpoint checkpoint = new Checkpoint();
				checkpoint.setData(longitudeTF.getText(), latitudeTF.getText());
				checkpoint.setDescription(descriptionTF.getText());
				
				this.addCheckpoint(checkpoint);

				longitudeTF.setEnabled(false);
				latitudeTF.setEnabled(false);
				descriptionTF.setEnabled(false);
				addCheckpointB.setText("Nyt");
			}
			catch (Models.EmptyValueException e)
			{
				informAboutEmptyFields();
				return;
			}
			catch (NumberFormatException e)
			{
				informAboutWrongInput("Længde- og breddegrad skal være kommatal.", "Forkert information");
				return;
			}
		}
	}//GEN-LAST:event_addCheckpointBActionPerformed

	private void removeCheckpointBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeCheckpointBActionPerformed
	{//GEN-HEADEREND:event_removeCheckpointBActionPerformed
		int checkpointNo = Integer.parseInt(checkpointTable.getValueAt(checkpointTable.getSelectedRow(),0).toString());
		Checkpoint checkpoint = new CheckpointController().find(checkpointNo);
		removeCheckpoint(checkpoint);
	}//GEN-LAST:event_removeCheckpointBActionPerformed

	private void searchBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBActionPerformed
		tabbedPane.search(TabIndex.Rounds, searchTF.getText());
	}//GEN-LAST:event_searchBActionPerformed

	private void chooseLocationBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseLocationBActionPerformed
		if(picker == null) {
			picker = createPicker(TabIndex.Locations);
			picker.addWindowListener(this);
			picker.setVisible(true);
		}
}//GEN-LAST:event_chooseLocationBActionPerformed

	private void cancelBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBActionPerformed
		clearComponents();
		changeComponentState(false);
	}//GEN-LAST:event_cancelBActionPerformed

	private void addCheckpoint(Checkpoint checkpoint)
	{
		Round theOne = getTheOne();

		checkpoint.setRoundNo(theOne.getRoundNo());
		theOne.addCheckpoint(checkpoint);
		refreshTable(theOne.getCheckpoints());
	}

	private void removeCheckpoint(Checkpoint checkpoint)
	{
		Round theOne = getTheOne();

		theOne.removeCheckpoint(checkpoint);
		refreshTable(theOne.getCheckpoints());
	}

	private void refreshTable(ArrayList<Checkpoint> checkpoints)
	{
		checkpointTable.setModel(new CheckpointTableModel(checkpoints));
	}

	public void changeComponentState(boolean state)
	{
		locationNoTF.setEnabled(state);
		chooseLocationB.setEnabled(state);
		showLocationB.setEnabled(chosenLocation != null);
		checkpointTable.setEnabled(state);
		cancelB.setEnabled(state);
		saveB.setEnabled(state);
		addCheckpointB.setEnabled(state);
		setUnsavedState(state);
	}

	public void populateFields()
	{
		roundNoTF.setText(String.valueOf(chosenOne.getRoundNo()));
		locationNoTF.setText(String.valueOf(chosenOne.getLocation().getLocationNo()));
		this.setChosenLocation(chosenOne.getLocation());
		showLocationB.setEnabled(chosenLocation != null);
		checkpointTable.setModel(new CheckpointTableModel(chosenOne.getCheckpoints()));
		newB.setEnabled(true);
		editB.setEnabled(true);
		deleteB.setEnabled(true);
	}

	public void clearComponents()
	{
		chosenOne = null;
		newOne = null;
		roundNoTF.setText("");
		this.setChosenLocation(null);
		showLocationB.setEnabled(false);
		locationNoTF.setText("");
		checkpointTable.setModel(new RoundTableModel());
		latitudeTF.setText("");
		longitudeTF.setText("");
		descriptionTF.setText("");
		addCheckpointB.setEnabled(false);
		removeCheckpointB.setEnabled(false);
		newB.setEnabled(true);
		editB.setEnabled(false);
		deleteB.setEnabled(false);
	}

	protected void showNo(int no)
	{
		chosenOne = controller.find(no);
		chosenOne.fetchSubItems();
		populateFields();
	}

	public void valueChanged(ListSelectionEvent e)
	{
		if(checkpointTable.getSelectedRowCount() == 1)
		{
			removeCheckpointB.setEnabled(true);
		}
	}

	public void windowClosing(WindowEvent e)
	{
		if(picker != null)
		{
			Location location = (Location)picker.getTheChosenOne();

			if(location != null)
			{
				this.setChosenLocation(location);
			}
			
			this.releasePicker();
		}
	}

	private void setChosenLocation(Location location)
	{
		this.chosenLocation = location;
		if(location != null)
		{
			locationNoTF.setText(String.valueOf(location.getLocationNo()));
			showLocationB.setEnabled(true);
		}
		else
		{
			locationNoTF.setText("");
			showLocationB.setEnabled(false);
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator Sep2;
    private javax.swing.JSeparator Sep3;
    private javax.swing.JButton addCheckpointB;
    private javax.swing.JButton cancelB;
    private javax.swing.JScrollPane checkpointSP;
    private javax.swing.JTable checkpointTable;
    private javax.swing.JButton chooseLocationB;
    private javax.swing.JButton deleteB;
    private javax.swing.JLabel descriptionL;
    private javax.swing.JTextField descriptionTF;
    private javax.swing.JButton editB;
    private javax.swing.JLabel latitudeL;
    private javax.swing.JTextField latitudeTF;
    private javax.swing.JLabel locationNoL;
    private javax.swing.JTextField locationNoTF;
    private javax.swing.JLabel longitudeL;
    private javax.swing.JTextField longitudeTF;
    private javax.swing.JButton newB;
    private javax.swing.JButton removeCheckpointB;
    private javax.swing.JLabel roundNoL;
    private javax.swing.JTextField roundNoTF;
    private javax.swing.JButton saveB;
    private javax.swing.JButton searchB;
    private javax.swing.JTextField searchTF;
    private javax.swing.JSeparator sep1;
    private javax.swing.JButton showAllB;
    private javax.swing.JButton showLocationB;
    // End of variables declaration//GEN-END:variables
}